home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / wind3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  15.4 KB  |  488 lines

  1. /*
  2.  * Copyright 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <GL/glx.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <X11/keysym.h>
  23.  
  24. static int RGB_attributes_DB[] = {
  25.     GLX_RGBA,
  26.     GLX_RED_SIZE, 1,
  27.     GLX_GREEN_SIZE, 1,
  28.     GLX_BLUE_SIZE, 1,
  29.     GLX_DOUBLEBUFFER,
  30.     None,
  31. };
  32.  
  33. static int CI_attributes_DB[] = {
  34.     GLX_DOUBLEBUFFER,
  35.     None,
  36. };
  37.  
  38. static int RGB_attributes[] = {
  39.     GLX_RGBA,
  40.     GLX_RED_SIZE, 1,
  41.     GLX_GREEN_SIZE, 1,
  42.     GLX_BLUE_SIZE, 1,
  43.     None,
  44. };
  45.  
  46. static int CI_attributes[] = {
  47.     None,
  48. };
  49.  
  50. int rgb = 0;
  51. int W = 300;
  52. int H = 300;
  53. int doubleBuf = 1;
  54. GLXContext cx, cx2;
  55. Window window, window2, window3;
  56. Display *dpy;
  57.  
  58. unsigned char font6x10[] = {
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  60. 0x00, 0x00, 0x10, 0x38, 0x38, 0x7c, 0x38, 0x38, 0x10, 0x00, 
  61. 0x00, 0x00, 0x54, 0x28, 0x54, 0x28, 0x54, 0x28, 0x54, 0x00, 
  62. 0x08, 0x08, 0x08, 0x3c, 0x48, 0x48, 0x78, 0x48, 0x48, 0x00, 
  63. 0x10, 0x10, 0x18, 0x10, 0x5c, 0x40, 0x60, 0x40, 0x70, 0x00, 
  64. 0x24, 0x24, 0x38, 0x24, 0x38, 0x38, 0x40, 0x40, 0x38, 0x00, 
  65. 0x20, 0x20, 0x38, 0x20, 0x3c, 0x78, 0x40, 0x40, 0x40, 0x00, 
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x28, 0x28, 0x38, 0x00, 
  67. 0x00, 0x00, 0x7c, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x00, 0x00, 
  68. 0x3c, 0x20, 0x20, 0x20, 0x48, 0x58, 0x68, 0x68, 0x48, 0x00, 
  69. 0x08, 0x08, 0x08, 0x08, 0x3c, 0x20, 0x30, 0x48, 0x48, 0x00, 
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x10, 0x10, 0x10, 0x10, 
  71. 0x10, 0x10, 0x10, 0x10, 0x10, 0x70, 0x00, 0x00, 0x00, 0x00, 
  72. 0x10, 0x10, 0x10, 0x10, 0x10, 0x1c, 0x00, 0x00, 0x00, 0x00, 
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x10, 0x10, 0x10, 0x10, 
  74. 0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x10, 0x10, 
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 
  78. 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  79. 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  80. 0x10, 0x10, 0x10, 0x10, 0x10, 0x1c, 0x10, 0x10, 0x10, 0x10, 
  81. 0x10, 0x10, 0x10, 0x10, 0x10, 0x70, 0x10, 0x10, 0x10, 0x10, 
  82. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x10, 0x10, 0x10, 0x10, 
  83. 0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00, 0x00, 0x00, 0x00, 
  84. 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 
  85. 0x00, 0x7c, 0x04, 0x08, 0x20, 0x40, 0x20, 0x08, 0x04, 0x00, 
  86. 0x00, 0x7c, 0x40, 0x20, 0x08, 0x04, 0x08, 0x20, 0x40, 0x00, 
  87. 0x00, 0x00, 0x28, 0x28, 0x28, 0x28, 0x7c, 0x00, 0x00, 0x00, 
  88. 0x00, 0x00, 0x40, 0x20, 0x7c, 0x10, 0x7c, 0x08, 0x04, 0x00, 
  89. 0x00, 0x20, 0x6c, 0x38, 0x20, 0x78, 0x20, 0x24, 0x18, 0x00, 
  90. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
  91. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  92. 0x00, 0x00, 0x10, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x28, 0x28, 0x00, 
  94. 0x00, 0x00, 0x28, 0x28, 0x7c, 0x28, 0x7c, 0x28, 0x28, 0x00, 
  95. 0x00, 0x00, 0x10, 0x38, 0x14, 0x38, 0x50, 0x38, 0x10, 0x00, 
  96. 0x00, 0x00, 0x48, 0x54, 0x28, 0x10, 0x28, 0x54, 0x24, 0x00, 
  97. 0x00, 0x00, 0x34, 0x48, 0x54, 0x20, 0x50, 0x50, 0x20, 0x00, 
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x10, 0x18, 0x00, 
  99. 0x00, 0x00, 0x08, 0x10, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00, 
  100. 0x00, 0x00, 0x20, 0x10, 0x08, 0x08, 0x08, 0x10, 0x20, 0x00, 
  101. 0x00, 0x00, 0x00, 0x44, 0x28, 0x7c, 0x28, 0x44, 0x00, 0x00, 
  102. 0x00, 0x00, 0x00, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x00, 0x00, 
  103. 0x00, 0x20, 0x10, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 
  105. 0x00, 0x10, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  106. 0x00, 0x00, 0x40, 0x40, 0x20, 0x10, 0x08, 0x04, 0x04, 0x00, 
  107. 0x00, 0x00, 0x10, 0x28, 0x44, 0x44, 0x44, 0x28, 0x10, 0x00, 
  108. 0x00, 0x00, 0x7c, 0x10, 0x10, 0x10, 0x50, 0x30, 0x10, 0x00, 
  109. 0x00, 0x00, 0x7c, 0x40, 0x20, 0x18, 0x04, 0x44, 0x38, 0x00, 
  110. 0x00, 0x00, 0x38, 0x44, 0x04, 0x18, 0x08, 0x04, 0x7c, 0x00, 
  111. 0x00, 0x00, 0x08, 0x08, 0x7c, 0x48, 0x28, 0x18, 0x08, 0x00, 
  112. 0x00, 0x00, 0x38, 0x44, 0x04, 0x64, 0x58, 0x40, 0x7c, 0x00, 
  113. 0x00, 0x00, 0x38, 0x44, 0x64, 0x58, 0x40, 0x20, 0x18, 0x00, 
  114. 0x00, 0x00, 0x20, 0x20, 0x10, 0x08, 0x08, 0x04, 0x7c, 0x00, 
  115. 0x00, 0x00, 0x38, 0x44, 0x44, 0x38, 0x44, 0x44, 0x38, 0x00, 
  116. 0x00, 0x00, 0x30, 0x08, 0x04, 0x34, 0x4c, 0x44, 0x38, 0x00, 
  117. 0x00, 0x10, 0x38, 0x10, 0x00, 0x10, 0x38, 0x10, 0x00, 0x00, 
  118. 0x00, 0x20, 0x10, 0x18, 0x00, 0x10, 0x38, 0x10, 0x00, 0x00, 
  119. 0x00, 0x00, 0x04, 0x08, 0x10, 0x20, 0x10, 0x08, 0x04, 0x00, 
  120. 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x00, 0x00, 
  121. 0x00, 0x00, 0x40, 0x20, 0x10, 0x08, 0x10, 0x20, 0x40, 0x00, 
  122. 0x00, 0x00, 0x10, 0x00, 0x10, 0x10, 0x08, 0x44, 0x38, 0x00, 
  123. 0x00, 0x00, 0x38, 0x40, 0x58, 0x54, 0x4c, 0x44, 0x38, 0x00, 
  124. 0x00, 0x00, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x28, 0x10, 0x00, 
  125. 0x00, 0x00, 0x78, 0x24, 0x24, 0x38, 0x24, 0x24, 0x78, 0x00, 
  126. 0x00, 0x00, 0x38, 0x44, 0x40, 0x40, 0x40, 0x44, 0x38, 0x00, 
  127. 0x00, 0x00, 0x78, 0x24, 0x24, 0x24, 0x24, 0x24, 0x78, 0x00, 
  128. 0x00, 0x00, 0x7c, 0x40, 0x40, 0x78, 0x40, 0x40, 0x7c, 0x00, 
  129. 0x00, 0x00, 0x40, 0x40, 0x40, 0x78, 0x40, 0x40, 0x7c, 0x00, 
  130. 0x00, 0x00, 0x38, 0x44, 0x4c, 0x40, 0x40, 0x44, 0x38, 0x00, 
  131. 0x00, 0x00, 0x44, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x44, 0x00, 
  132. 0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x10, 0x10, 0x38, 0x00, 
  133. 0x00, 0x00, 0x38, 0x44, 0x04, 0x04, 0x04, 0x04, 0x1c, 0x00, 
  134. 0x00, 0x00, 0x44, 0x48, 0x50, 0x60, 0x50, 0x48, 0x44, 0x00, 
  135. 0x00, 0x00, 0x7c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 
  136. 0x00, 0x00, 0x44, 0x44, 0x44, 0x54, 0x6c, 0x44, 0x44, 0x00, 
  137. 0x00, 0x00, 0x44, 0x44, 0x4c, 0x54, 0x64, 0x44, 0x44, 0x00, 
  138. 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00, 
  139. 0x00, 0x00, 0x40, 0x40, 0x40, 0x78, 0x44, 0x44, 0x78, 0x00, 
  140. 0x00, 0x04, 0x38, 0x54, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00, 
  141. 0x00, 0x00, 0x44, 0x48, 0x50, 0x78, 0x44, 0x44, 0x78, 0x00, 
  142. 0x00, 0x00, 0x38, 0x44, 0x04, 0x38, 0x40, 0x44, 0x38, 0x00, 
  143. 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00, 
  144. 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x00, 
  145. 0x00, 0x00, 0x10, 0x28, 0x28, 0x28, 0x44, 0x44, 0x44, 0x00, 
  146. 0x00, 0x00, 0x44, 0x6c, 0x54, 0x54, 0x44, 0x44, 0x44, 0x00, 
  147. 0x00, 0x00, 0x44, 0x44, 0x28, 0x10, 0x28, 0x44, 0x44, 0x00, 
  148. 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x28, 0x44, 0x44, 0x00, 
  149. 0x00, 0x00, 0x7c, 0x40, 0x20, 0x10, 0x08, 0x04, 0x7c, 0x00, 
  150. 0x00, 0x00, 0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x38, 0x00, 
  151. 0x00, 0x00, 0x04, 0x04, 0x08, 0x10, 0x20, 0x40, 0x40, 0x00, 
  152. 0x00, 0x00, 0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 
  153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x28, 0x10, 0x00, 
  154. 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  155. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x30, 0x00, 
  156. 0x00, 0x00, 0x3c, 0x44, 0x3c, 0x04, 0x38, 0x00, 0x00, 0x00, 
  157. 0x00, 0x00, 0x58, 0x64, 0x44, 0x64, 0x58, 0x40, 0x40, 0x00, 
  158. 0x00, 0x00, 0x38, 0x44, 0x40, 0x44, 0x38, 0x00, 0x00, 0x00, 
  159. 0x00, 0x00, 0x34, 0x4c, 0x44, 0x4c, 0x34, 0x04, 0x04, 0x00, 
  160. 0x00, 0x00, 0x38, 0x40, 0x7c, 0x44, 0x38, 0x00, 0x00, 0x00, 
  161. 0x00, 0x00, 0x20, 0x20, 0x20, 0x78, 0x20, 0x24, 0x18, 0x00, 
  162. 0x38, 0x44, 0x38, 0x40, 0x30, 0x48, 0x34, 0x00, 0x00, 0x00, 
  163. 0x00, 0x00, 0x44, 0x44, 0x44, 0x64, 0x58, 0x40, 0x40, 0x00, 
  164. 0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x30, 0x00, 0x10, 0x00, 
  165. 0x30, 0x48, 0x48, 0x08, 0x08, 0x08, 0x18, 0x00, 0x08, 0x00, 
  166. 0x00, 0x00, 0x44, 0x48, 0x70, 0x48, 0x44, 0x40, 0x40, 0x00, 
  167. 0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x10, 0x10, 0x30, 0x00, 
  168. 0x00, 0x00, 0x44, 0x54, 0x54, 0x54, 0x68, 0x00, 0x00, 0x00, 
  169. 0x00, 0x00, 0x44, 0x44, 0x44, 0x64, 0x58, 0x00, 0x00, 0x00, 
  170. 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00, 
  171. 0x40, 0x40, 0x40, 0x58, 0x64, 0x64, 0x58, 0x00, 0x00, 0x00, 
  172. 0x04, 0x04, 0x04, 0x34, 0x4c, 0x4c, 0x34, 0x00, 0x00, 0x00, 
  173. 0x00, 0x00, 0x40, 0x40, 0x40, 0x64, 0x58, 0x00, 0x00, 0x00, 
  174. 0x00, 0x00, 0x78, 0x04, 0x38, 0x40, 0x38, 0x00, 0x00, 0x00, 
  175. 0x00, 0x00, 0x18, 0x24, 0x20, 0x20, 0x78, 0x20, 0x20, 0x00, 
  176. 0x00, 0x00, 0x34, 0x4c, 0x44, 0x44, 0x44, 0x00, 0x00, 0x00, 
  177. 0x00, 0x00, 0x10, 0x28, 0x28, 0x44, 0x44, 0x00, 0x00, 0x00, 
  178. 0x00, 0x00, 0x28, 0x54, 0x54, 0x44, 0x44, 0x00, 0x00, 0x00, 
  179. 0x00, 0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00, 0x00, 0x00, 
  180. 0x38, 0x44, 0x04, 0x34, 0x4c, 0x44, 0x44, 0x00, 0x00, 0x00, 
  181. 0x00, 0x00, 0x7c, 0x20, 0x10, 0x08, 0x7c, 0x00, 0x00, 0x00, 
  182. 0x00, 0x00, 0x0c, 0x10, 0x08, 0x30, 0x08, 0x10, 0x0c, 0x00, 
  183. 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 
  184. 0x00, 0x00, 0x60, 0x10, 0x20, 0x18, 0x20, 0x10, 0x60, 0x00, 
  185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x54, 0x24, 0x00, 
  186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  187. };
  188.  
  189. int fontbase = 256;
  190.  
  191. #define MAX_COLORS 15
  192.  
  193. float colors[MAX_COLORS][3] = {
  194.     { 1.0, 0.0, 0.0 },
  195.     { 0.8, 0.2, 0.0 },
  196.     { 0.6, 0.4, 0.0 },
  197.     { 0.4, 0.6, 0.0 },
  198.     { 0.2, 0.8, 0.0 },
  199.     { 0.0, 1.0, 0.0 },
  200.     { 0.0, 0.8, 0.2 },
  201.     { 0.0, 0.6, 0.4 },
  202.     { 0.0, 0.4, 0.6 },
  203.     { 0.0, 0.2, 0.8 },
  204.     { 0.0, 0.0, 1.0 },
  205.     { 0.2, 0.0, 0.8 },
  206.     { 0.4, 0.0, 0.6 },
  207.     { 0.6, 0.0, 0.4 },
  208.     { 0.8, 0.0, 0.2 },
  209. };
  210.  
  211. void initializeFont(void)
  212. {
  213.     int i;
  214.  
  215.     glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
  216.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  217.  
  218.     for (i=0; i<128; i++) {
  219.     glNewList(i+fontbase, GL_COMPILE);
  220.     glBitmap(6, 10, 0.0, 0.0, 6.0, 0.0, font6x10+10*i);
  221.     glEndList();
  222.     }
  223. }
  224.  
  225. void writeFont(char *string)
  226. {
  227.     glListBase(fontbase);
  228.     glCallLists(strlen(string), GL_BYTE, (unsigned char *) string);
  229. }
  230.  
  231. static void Init(void)
  232. {
  233.     glViewport(0,0,W,H);
  234.     glMatrixMode(GL_PROJECTION);
  235.     glLoadIdentity();
  236.     glOrtho(0,W,0,H,-1,1);
  237.     glMatrixMode(GL_MODELVIEW);
  238.  
  239.     if (rgb) {
  240.     glClearColor(0,0,0,0);
  241.     } else {
  242.     glClearIndex(0);
  243.     }
  244.  
  245.     glClear(GL_COLOR_BUFFER_BIT);
  246.  
  247.     initializeFont();
  248. }
  249.  
  250. static void Redraw(void)
  251. {
  252.     int i, y;
  253.     static int color;
  254.     static int which;
  255.  
  256.     color = (color+1) % MAX_COLORS;
  257.     for (i=0; i<3; i++) {
  258.     if (i == 0) {
  259.         glXMakeCurrent(dpy, window, cx);
  260.     } else if (i == 1) {
  261.         which = 1-which;
  262.         if (which) {
  263.         glXMakeCurrent(dpy, window2, cx2);
  264.         } else {
  265.         glXMakeCurrent(dpy, window2, cx);
  266.         }
  267.     } else {
  268.         glXMakeCurrent(dpy, window3, cx2);
  269.     }
  270.  
  271.     if (rgb) {
  272.         glColor3fv(colors[color]);
  273.     } else {
  274.         glIndexi(color);
  275.     }
  276.  
  277.     glClear(GL_COLOR_BUFFER_BIT);
  278.  
  279.     glRasterPos2i(5, 50);
  280.     writeFont("Hello");
  281.     sginap(10);
  282.     glRasterPos2i(5, 40);
  283.     writeFont("There");
  284.     sginap(10);
  285.     glRasterPos2i(5, 30);
  286.     writeFont("World");
  287.  
  288.     if (doubleBuf) {
  289.         if (i == 0) {
  290.         glXSwapBuffers(dpy, window);
  291.         } else if (i == 1) {
  292.         glXSwapBuffers(dpy, window2);
  293.         } else {
  294.         glXSwapBuffers(dpy, window3);
  295.         }
  296.     }
  297.     }
  298. }
  299.  
  300. static void Usage(void)
  301. {
  302.     fprintf(stderr, "Usage: wind2 [-c] [-s]\n");
  303.     fprintf(stderr, " -c  run in color index mode\n");
  304.     fprintf(stderr, " -s  run in singlebuffer mode\n");
  305.     exit(1);
  306. }
  307.  
  308. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  309. {
  310.     if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
  311.     return GL_TRUE;
  312.     }
  313.     return GL_FALSE;
  314. }
  315.  
  316. int main(int argc, char** argv)
  317. {
  318.     XVisualInfo *vi;
  319.     Colormap cmap;
  320.     XSetWindowAttributes swa;
  321.     XEvent event;
  322.     XColor white;
  323.     int i;
  324.  
  325.     rgb = 1;
  326.     doubleBuf = 1;
  327.     for (i = 1; i < argc; i++) {
  328.         if (argv[i][0] == '-') {
  329.             switch (argv[i][1]) {
  330.               case 'c':
  331.                 rgb = GL_FALSE;
  332.                 break;
  333.           case 's':
  334.         doubleBuf = 0;
  335.         break;
  336.               default:
  337.                 Usage();
  338.             }
  339.         } else {
  340.             Usage();
  341.         }
  342.     }
  343.  
  344.     dpy = XOpenDisplay(0);
  345.     if (!dpy) {
  346.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  347.     return -1;
  348.     }
  349.  
  350.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  351.              doubleBuf ? 
  352.              (rgb ? RGB_attributes_DB : CI_attributes_DB) :
  353.              (rgb ? RGB_attributes : CI_attributes));
  354.     if (!vi) {
  355.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  356.         getenv("DISPLAY"));
  357.     return -1;
  358.     }
  359.  
  360.     printf("Drawing with two contexts, three windows.\n");
  361.     if (rgb) {
  362.     printf("rgb mode, ");
  363.     } else {
  364.     printf("color index mode, ");
  365.     }
  366.     if (doubleBuf) {
  367.     printf("double buffered\n");
  368.     } else {
  369.     printf("single buffered\n");
  370.     }
  371.  
  372.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  373.                rgb ? AllocNone : AllocAll);
  374.     if (rgb) {
  375.     white.red = ~0;
  376.     white.green = ~0;
  377.     white.blue = ~0;
  378.     XAllocColor(dpy, cmap, &white);
  379.     swa.background_pixel = white.pixel;
  380.     } else {
  381.     swa.background_pixel = 15;
  382.     }
  383.  
  384.     if (!rgb) {
  385.     XColor buf;
  386.     int i;
  387.  
  388.     buf.flags = DoRed | DoGreen | DoBlue;
  389.  
  390.     /* Init color map */
  391.     for (i=0; i<16; i++) {
  392.         buf.pixel = i;
  393.         buf.red = (i & 4) ? 65535 : 0;
  394.         buf.green = (i & 2) ? 65535 : 0;
  395.         buf.blue = (i & 1) ? 65535 : 0;
  396.         XStoreColor(dpy, cmap, &buf);
  397.     }
  398.     }
  399.  
  400.     swa.border_pixel = 0;
  401.     swa.colormap = cmap;
  402.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  403.     | KeyReleaseMask;
  404.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  405.                W, H,
  406.                0, vi->depth, InputOutput, vi->visual,
  407.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  408.                &swa);
  409.     XSetWMColormapWindows(dpy, window, &window, 1);
  410.     XMapWindow(dpy, window);
  411.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  412.  
  413.     window3 = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  414.                W, H,
  415.                0, vi->depth, InputOutput, vi->visual,
  416.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  417.                &swa);
  418.     XSetWMColormapWindows(dpy, window3, &window3, 1);
  419.     XMapWindow(dpy, window3);
  420.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window3);
  421.  
  422.     window2 = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  423.                W, H,
  424.                0, vi->depth, InputOutput, vi->visual,
  425.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  426.                &swa);
  427.     XSetWMColormapWindows(dpy, window2, &window2, 1);
  428.     XMapWindow(dpy, window2);
  429.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window2);
  430.  
  431.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  432.     if (!glXMakeCurrent(dpy, window, cx)) {
  433.     fprintf(stderr, "Can't make window current to context\n");
  434.     return -1;
  435.     }
  436.     Init();
  437.  
  438.     cx2 = glXCreateContext(dpy, vi, 0, GL_TRUE);
  439.     if (!glXMakeCurrent(dpy, window, cx2)) {
  440.     fprintf(stderr, "Can't make window current to context\n");
  441.     return -1;
  442.     }
  443.     Init();
  444.  
  445.     for (;;) {
  446.     if (XPending(dpy)) do {
  447.         XNextEvent(dpy, &event);
  448.         switch (event.type) {
  449.           case Expose:
  450.         break;
  451.           case ConfigureNotify:
  452.         W = event.xconfigure.width;
  453.         H = event.xconfigure.height;
  454.         glViewport(0,0,W,H);
  455.         glMatrixMode(GL_PROJECTION);
  456.         glLoadIdentity();
  457.         glOrtho(0,W,0,H,-1,1);
  458.         glMatrixMode(GL_MODELVIEW);
  459.         glClear(GL_COLOR_BUFFER_BIT);
  460.         break;
  461.           case KeyPress:
  462.         {
  463.             char buf[100];
  464.             int rv;
  465.             KeySym ks;
  466.  
  467.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  468.             switch (ks) {
  469.               case XK_F: case XK_f:
  470.               case XK_S: case XK_s:
  471.               case XK_P: case XK_p:
  472.               case XK_L: case XK_l:
  473.               case XK_C: case XK_c:
  474.             break;
  475.               case XK_Escape:
  476.             return 0;
  477.               default:
  478.             break;
  479.             }
  480.         }
  481.         break;
  482.         }
  483.     } while (XPending(dpy) != 0);
  484.  
  485.     Redraw();
  486.     }
  487. }
  488.